$(function () {

    var $tableIns = $("#table-funcs"),
        $funcsForm = $("#funcs-form"),
        $dialogFuncsEdit = $("#dialog-funcs-edit");

    // 加载接口列表
    $tableIns.datagrid({
        url: _ctx + "/sysFuncs/list",
        idField: "id",
        rownumbers: true,
        border: false,
        toolbar: "#toolbar",
        checkOnSelect: false,
        selectOnCheck: false,
        fitColumns: true,
        singleSelect: true,
        fit: true,
        frozenColumns: [[
            {field: '', title: '', checkbox: true},
            {field: 'id', title: '标识', width: 280}
        ]],
        columns: [[
            {field: 'funcName', title: '功能名称', width: 350},
            {field: 'funcIcon', title: '功能图标', width: 250},
            {field: 'permissions', title: '权限代码', width: 250}
        ]]
    });

    $("#add").click(function () {
        $dialogFuncsEdit.dialog({
            title: "新增",
            iconCls: "icon-add",
            buttons: [{
                text: "确定",
                iconCls: "icon-ok",
                handler: function () {
                    var validateResult = $funcsForm.form("validate");
                    if (validateResult) {
                        $.post(_ctx + "/sysFuncs/insert", $funcsForm.serializeArray(), function (result) {
                            if (result.success) {
                                $dialogFuncsEdit.dialog("close");
                                $.messager.alert("提示", result.message, "info", function () {
                                    $tableIns.datagrid("reload");
                                    $tableIns.datagrid("uncheckAll")
                                });
                            } else {
                                $.messager.alert("提示", result.message, "info");
                            }
                        }, "json");
                    }
                }
            }, {
                text: "取消",
                iconCls: "icon-cancel",
                handler: function () {
                    $dialogFuncsEdit.dialog("close");
                }
            }]
        });
        $funcsForm.form("clear");
        $dialogFuncsEdit.dialog("open");
    });

    $("#update").click(function () {
        var dataRows = $tableIns.datagrid("getSelections");
        if (dataRows.length === 1) {
            $dialogFuncsEdit.dialog({
                title: "修改",
                iconCls: "icon-pencil",
                buttons: [{
                    text: "确定",
                    iconCls: "icon-ok",
                    handler: function () {
                        var validateResult = $funcsForm.form("validate");
                        if (validateResult) {
                            $.post(_ctx + "/sysFuncs/update", $funcsForm.serializeArray(), function (result) {
                                if (result.success) {
                                    $dialogFuncsEdit.dialog("close");
                                    $.messager.alert("提示", result.message, "info", function () {
                                        $tableIns.datagrid("reload");
                                        $tableIns.datagrid("uncheckAll")
                                    });
                                } else {
                                    $.messager.alert("提示", result.message, "info");
                                }
                            }, "json");
                        }
                    }
                }, {
                    text: "取消",
                    iconCls: "icon-cancel",
                    handler: function () {
                        $dialogFuncsEdit.dialog("close");
                    }
                }]
            });
            $funcsForm.form("load", dataRows[0]);
            $dialogFuncsEdit.dialog("open");
        }
    });

    $("#delete").click(function () {
        var dataRows = $tableIns.datagrid("getChecked");
        if (dataRows.length > 0) {
            $.messager.confirm("提示", "确定要删除选中的" + dataRows.length + "行 ?", function (r) {
                if (r) {
                    var idArr = [];
                    for (var i = 0; i < dataRows.length; i++) {
                        idArr.push(dataRows[i].id);
                    }
                    $.post(_ctx + "/sysFuncs/delete", {ids: idArr.join(",")}, function (result) {
                        if (result.success) {
                            $dialogFuncsEdit.dialog("close");
                            $.messager.alert("提示", result.message, "info", function () {
                                $tableIns.datagrid("reload");
                                $tableIns.datagrid("uncheckAll")
                            });
                        } else {
                            $.messager.alert("提示", result.message, "info");
                        }
                    }, "json");
                }
            });
        }
    });
});